package com.ygh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygh.mapper.AdminNoticeMapper;
import com.ygh.pojo.Notice;
import com.ygh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminNoticeServiceImpl implements AdminNoticeService {
    @Autowired(required = false)
    private AdminNoticeMapper adminNoticeMapper;

    @Override
    public PageResult findNoticeList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"title",pageResult.getQuery());
        adminNoticeMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Notice> noticeList = page.getRecords();
        return pageResult.setTotal(total).setRows(noticeList);
    }

    @Override
    public List<Notice> findAll() {
        return adminNoticeMapper.selectList(null);
    }

    @Override
    public void saveNotice(Notice notice) {
        adminNoticeMapper.insert(notice);
    }

    @Override
    public void deleteNotice(Integer id) {
        adminNoticeMapper.deleteById(id);
    }

    @Override
    public List<Notice> findTitleList() {
        List list = new ArrayList();

        List<Notice> noticeList = adminNoticeMapper.selectList(null);
        for (Notice titleList:noticeList){
            String s = titleList.getContent();
            list.add(s);
        }

        return list;
    }

    @Override
    public void updateNotice(Notice notice) {
        adminNoticeMapper.updateById(notice);
    }

}
